$(function(){
	//输入框正则验证
	$('.order_user_tel').blur(function() {
		reg = /^[1]{1}[0-9]{10}$/;
		tel = $(this).val();
		if(!reg.test(tel)){
			$('.telok').hide();
			$('.tel').html('<em></em>手机号码格式不正确').show();
		}else{
			$('.tel').hide();
			$('.telok').html('<em></em>').show();
			
		}
	});	

	//收件人验证
	$(".order_receiver").blur(function(){
		if( $(this).val() ==''){
			$('.user_receiverok').hide();
			$('.user_receiver').html('<em></em>收件人不能为空').show();
		}else{
			$('.user_receiver').hide();
			$('.user_receiverok').html('<em></em>').show();
		}
	})

		//详细地址
	$(".order_address").blur(function(){
		if( $(this).val() ==''){
			$('.order_addressok').hide();
			$('.order_addressalert').html('<em></em>详细地址不能为空').show();
		}else{
			$('.order_addressalert').hide();
			$('.order_addressok').html('<em></em>').show();
		}
	})

	//提交配送地址
	$('.besureAddress').click(function(){
		//判断必填项是否填写完成
		
		//手机号码验证
		reg = /^[1]{1}[0-9]{10}$/;
		tel = $('.order_user_tel').val();
		if(!reg.test(tel)){
			$('.telok').hide();
			$('.tel').html('<em></em>手机号码格式不正确').show();
			return false;
		}else{
			$('.tel').hide();
			$('.telok').html('<em></em>').show();			
		}

		//收件人验证
		if($(".order_receiver").val() ==''){
			$('.user_receiverok').hide();
			$('.user_receiver').html('<em></em>收件人不能为空').show();
			return false;
		}else{
			$('.user_receiver').hide();
			$('.user_receiverok').html('<em></em>').show();
		}

		//详细地址
		if( $(".order_address").val() ==''){
			$('.order_addressok').hide();
			$('.order_addressalert').html('<em></em>详细地址不能为空').show();
			return false;
		}else{
			$('.order_addressalert').hide();
			$('.order_addressok').html('<em></em>').show();
		}

		//抓取内容
		order_receiver = $(".order_receiver").val();//收件人
		tel = $('.order_user_tel').val();
		phone = $("input[name='order_phone']").val();//电话
		place = $("input[name='order_user_place']").val();//所在地区
		order_address = $(".order_address").val(); //详细地址
		$.ajax({
			data:'order_receiver='+order_receiver+'&tel='+tel+'&phone='+phone+'&place='+place+'&order_address='+order_address,
			type:'post',
			dataType:'json',
			url:controller+"/saveaddress",
			success:function(msg){
				if(msg != '' ){
					//隐藏显示配送信息
					$('.addNewAddress').hide();
					var str = "<li class='checkAddress sendto' addressid ='"+msg+"'><span class='posi'></span>寄送至： <input type='radio' name='sendto' /> "+order_receiver+" "+tel+"   "+place+" "+order_address+" <a class='gobackgShopping makedefault' href='javascript:;' addressid ='"+msg+"'>设为默认地址</a> <a class='gobackgShopping' href='javascript:;' addressid ='"+msg+"' >修改</a> <a href='javascript:delAddress("+msg+");'>删除</a></li>";
					$('.addAddressbtn').before(str);
				}

			}
		})



	})
	

	//加载显示与隐藏
	if($('.sendto').length >0){
		$('.addNewAddress').hide();
	}else{
		$('.addNewAddress').show();
	}

	//添加地址
	$('.addAddressbtn').click(function(){
		$('.userInput input:[type=text]').val('');
		$('.add_address_btn').attr('checked',false);
		$('.addNewAddress').show();
	})
	//选择后 添加地址隐藏
	$('input[name=sendto]').click(function(){
		$('.nogoodschoose').hide();
		$('.addNewAddress').hide();
	})

	// 提交
	$('.submit').click(function(){
		if($(':checked').length != 2){
			$('.nogoodschoose').show();
			return false;
		}
	sendaddress = $('input[name="sendto"]:checked ').val();
	payway = $('input[name="order_payway"]:checked ').val();

	$('.loading').show();
	 //发送处理
	 $.ajax({
		data:'sendaddress='+sendaddress +'&payway='+payway,
		type:'post',
		dataType:'json',
		url:controller+"/makeOrder",
		success:function(msg){
			if(msg != ''){
				$('.loading').hide();
				$('.success').show();
				location.href =controller+"/mycarStep_3/oid/"+msg.oid;		
			}
		
		}
	 })
	})

	//设为默认地址
	$('.makedefault').click(function(){
		i = $(this).index('.makedefault');
		addressid = $(this).attr('addressid');
		$.ajax({
			data:'addressid='+addressid,
			type:'post',
			dataType:'json',
			url:root + '/index.php/index/Member/addressDefault',
			success:function(msg){
				$('.makedefault').html('设为默认地址');
				$('.makedefault').eq(i).html('默认地址').css({'color':'#666'});
			}
		})
	})

	//修改地址 异步请求数据
 	$('.editAddress').click(function(){
		editNum = $(this).index('.editAddress');
		addressid = $(this).attr('addressid');
		$.ajax({
			data:'addressid='+addressid,
			type:'post',
			dataType:'json',
			url:root + '/index.php/index/Member/addressEdit',
			success:function(msg){
				//组合返回信息
				$('.order_receiver').val(msg.user_address_name);
				$('.order_user_tel').val(msg.user_address_tel);
				$('.order_phone').val(msg.user_address_phone);
				$('.order_user_place').val(msg.user_address_place);
				$('.order_address').val(msg.user_address_all);
				$('.besureAddress').hide();
				$('.editAddressbtn').attr('addressid',addressid).show();
				$('.addNewAddress').show();
			}
		})
	})

	//异步修改提交
 	$('.editAddressbtn').click(function(){
 		addressid = $(this).attr('addressid');
		//抓取内容
		order_receiver = $(".order_receiver").val();//收件人
		tel = $('.order_user_tel').val();
		phone = $("input[name='order_phone']").val();//电话
		place = $("input[name='order_user_place']").val();//所在地区
		order_address = $(".order_address").val(); //详细地址
		$.ajax({
			data:'order_receiver='+order_receiver+'&tel='+tel+'&phone='+phone+'&place='+place+'&order_address='+order_address+'&addressid='+addressid,
			type:'post',
			dataType:'json',
			url:root + "/index.php/index/Member/updateAddress",
			success:function(msg){
				if(msg != '' ){
					//隐藏显示配送信息
					$('.addNewAddress').hide();
					var str = "<span class='posi'></span>寄送至： <input type='radio' name='sendto' /> "+order_receiver+" "+tel+"   "+place+" "+order_address+" <a class='gobackgShopping makedefault' href='javascript:;' addressid ='"+msg+"'>设为默认地址</a> <a class='gobackgShopping' href='javascript:;' addressid ='"+msg+"' >修改</a> <a href='javascript:delAddress("+msg+");'>删除</a>";
					$('.sendto').eq(editNum).html(str);
				}

			}
		})
	})

	//删除地址
	$('.delAddress').click(function(){
		i = $(this).index('.delAddress');
		addressid = $(this).attr('addressid');
		$.ajax({
			data:'addressid='+addressid,
			type:'post',
			dataType:'json',
			url:root + '/index.php/index/Member/delAddress',
			success:function(msg){
				$('.sendto').eq(i).remove();
			}
		})
	})

	//取消添加修改	
	$('.quit').click(function(){
		$('.addNewAddress').hide();
	})




})

